package com.example.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.api.ResultCode;
import com.example.common.api.ResultUtil;
import com.example.goods.mapper.GoodsClassifyMapper;
import com.example.goods.mapper.GoodsMapper;
import com.example.goods.model.dto.GoodsDTO;
import com.example.goods.model.po.GoodsClassifyPO;
import com.example.goods.model.po.GoodsPO;
import com.example.goods.model.vo.GoodsVO;
import com.example.goods.service.IGoodsService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class GoodsService implements IGoodsService {

    @Resource
    private GoodsMapper goodsMapper;

    @Resource
    private GoodsClassifyMapper goodsClassifyMapper;

    @Override
    public ResultUtil createGoods(GoodsVO goodsVO) {
        log.info("create goods begin, receive params: {}", goodsVO.toString());

        GoodsPO goodsPO = new GoodsPO();
        BeanUtils.copyProperties(goodsVO, goodsPO);

        int row = goodsMapper.insert(goodsPO);
        if (row > 0){
            GoodsDTO goodsDTO = new GoodsDTO();
            BeanUtils.copyProperties(goodsPO, goodsDTO);

            log.info("create goods success end!");
            return ResultUtil.success(goodsDTO);
        }

        log.error("create goods fail, {}", goodsVO.toString());

        return ResultUtil.failed(ResultCode.CREATE_GOODS_ERROR);
    }

    @Override
    public ResultUtil getGoodsByPage(int pageIndex, int pageSize, GoodsVO goodsVO) {
        log.info("get goods by page begin, receive params: pageIndex: {}, pageSize: {}, goodsVO: {}", pageIndex, pageSize, goodsVO.toString());

        GoodsPO goodsPO = new GoodsPO();
        BeanUtils.copyProperties(goodsVO, goodsPO);

        Page<GoodsPO> page = new Page<>(pageIndex, pageSize);
        QueryWrapper<GoodsPO> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(goodsPO.getName())){
            queryWrapper.like("name", goodsPO.getName());
        }

        IPage<GoodsPO> iPage = goodsMapper.selectPage(page, queryWrapper);

        IPage<GoodsDTO> iPageDTO = iPage.convert(goodsPO1 -> {
            GoodsDTO goodsDTO = new GoodsDTO();
            BeanUtils.copyProperties(goodsPO1, goodsDTO);
            return goodsDTO;
        });

        log.info("get goods by page end");

        return ResultUtil.success(iPageDTO);
    }

    @Override
    public ResultUtil getGoodsByClassify(int pageIndex, int pageSize, String classifyName) {
        log.info("get goods by classifyName begin, receive params: pageIndex: {}, pageSize: {}, classifyName: {}", pageIndex, pageSize, classifyName);
        Map<String, Object> map = new HashMap<>();
        map.put("name", classifyName);
        QueryWrapper<GoodsClassifyPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.allEq(map);
        GoodsClassifyPO goodsClassifyPO = goodsClassifyMapper.selectOne(queryWrapper);

        Page<GoodsPO> page = new Page<>(pageIndex, pageSize);
        QueryWrapper<GoodsPO> queryWrapperGoods = new QueryWrapper<>();
        Map<String, Object> mapGoods = new HashMap<>();
        mapGoods.put("classify_id", goodsClassifyPO.getId());
        queryWrapperGoods.allEq(mapGoods);

        IPage<GoodsPO> iPage = goodsMapper.selectPage(page, queryWrapperGoods);

        IPage<GoodsDTO> iPageDTO = iPage.convert(goodsPO -> {
            GoodsDTO goodsDTO = new GoodsDTO();
            BeanUtils.copyProperties(goodsPO, goodsDTO);
            return goodsDTO;
        });

        log.info("get goods by classifyName end");
        return ResultUtil.success(iPageDTO);
    }

    @Override
    public ResultUtil updateGoodsById(GoodsVO goodsVO) {
        log.info("update goods by ID begin, receive params: {}", goodsVO.toString());

        GoodsPO goodsPO = new GoodsPO();
        BeanUtils.copyProperties(goodsVO, goodsPO);

        GoodsPO selectGoods = goodsMapper.selectById(goodsPO.getId());
        if (selectGoods == null){
            return ResultUtil.failed("修改的商品ID不存在");
        }

        int row = goodsMapper.updateById(goodsPO);

        if (row > 0){
            log.info("update goods by ID success, good_id: {}", goodsPO.getId());
            Map<String, Object> map = new HashMap<>();
            map.put("result", true);
            map.put("message", "修改信息成功");
            return ResultUtil.success(map);
        }

        log.error("update goods by ID fail, good_id: {}", goodsPO.getId());
        return ResultUtil.failed("修改商品失败");
    }

    @Override
    public ResultUtil getGoodsById(Long id) {
        GoodsPO goodsPO = goodsMapper.selectById(id);
        return ResultUtil.success(goodsPO);
    }
}
